/*
 * GuitarPanel.java
 *
 * Created on September 14, 2005, 1:02 PM
 */
package com.bff.mpd.musicjukebox.ui;

import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.musicjukebox.lib.MusicPlayerManager;
import com.bff.mpd.musicjukebox.lib.actions.OpenTabAction;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.ui.component.BJJButton;
import com.bff.mpd.utils.Utils;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.bff.javampd.MPDPlayer;
import org.bff.javampd.events.PlayerChangeEvent;
import org.bff.javampd.events.PlayerChangeListener;
import org.bff.javampd.events.PlaylistBasicChangeEvent;
import org.bff.javampd.events.PlaylistBasicChangeListener;
import org.bff.javampd.events.TrackPositionChangeEvent;
import org.bff.javampd.events.TrackPositionChangeListener;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDPlayerException;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author  bill
 */
public class GuitarPanel
        extends BJJPanel
        implements PlayerChangeListener,
        PlaylistBasicChangeListener,
        TrackPositionChangeListener {

    private MPDPlayer player;
    private Action openFileAction;
    private JTextArea tablatureTextArea;
    private boolean timeSliderStartPaused;
    private boolean timeSliderEndPaused;
    private boolean looping;
    private MusicPlayerManager musicPlayerManager;

    /** Creates new form GuitarPanel */
    public GuitarPanel() {
        setPlayer(JukeboxProperties.getInstance().getPlayer());

        this.musicPlayerManager = new MusicPlayerManager();
        this.tablatureTextArea = new JTextArea();

        this.openFileAction = new OpenTabAction(this, tablatureTextArea);

        initComponents();

        //JukeboxProperties.getInstance().getMPDMonitor().addPlayerChangeListener(this);
        JukeboxProperties.getInstance().getMPDMonitor().addTrackPositionChangeListener(this);
        JukeboxProperties.getInstance().getMPDMonitor().addPlaylistChangeListener(this);
        JukeboxProperties.getInstance().getPlayer().addPlayerChangeListener(this);

        startTimeSlider.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent event) {
                startTimeLabel.setText(Utils.formatTime(startTimeSlider.getValue()));
                verifyEndTime();
            }
        });

        endTimeSlider.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent event) {
                endTimeLabel.setText(Utils.formatTime(endTimeSlider.getValue()));
                verifyEndTime();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new BJJPanel();
        endTimeLabel = new javax.swing.JLabel();
        startTimeLabel = new javax.swing.JLabel();
        endTimeSlider = new javax.swing.JSlider();
        startTimeSlider = new javax.swing.JSlider();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btnPauseStart = new BJJButton();
        btnPauseEnd = new BJJButton();
        btnLoop = new BJJButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabTextArea = tablatureTextArea;
        jPanel2 = new BJJPanel();
        btnOpenFile = new javax.swing.JButton(openFileAction);

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        endTimeLabel.setText("0:00");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 1, 5, 5);
        jPanel1.add(endTimeLabel, gridBagConstraints);

        startTimeLabel.setText("0:00");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 1, 5, 5);
        jPanel1.add(startTimeLabel, gridBagConstraints);

        endTimeSlider.setValue(0);
        endTimeSlider.setOpaque(false);
        endTimeSlider.setPreferredSize(new java.awt.Dimension(250, 25));
        endTimeSlider.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                endSliderMouseReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(endTimeSlider, gridBagConstraints);

        startTimeSlider.setValue(0);
        startTimeSlider.setOpaque(false);
        startTimeSlider.setPreferredSize(new java.awt.Dimension(250, 25));
        startTimeSlider.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                startSliderMouseReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(startTimeSlider, gridBagConstraints);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel5.setText("Start:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(jLabel5, gridBagConstraints);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel6.setText("End:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(jLabel6, gridBagConstraints);

        btnPauseStart.setText("Pause");
        btnPauseStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPauseStartActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(btnPauseStart, gridBagConstraints);

        btnPauseEnd.setText("Pause");
        btnPauseEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPauseEndActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(btnPauseEnd, gridBagConstraints);

        btnLoop.setText("Loop");
        btnLoop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoopActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(btnLoop, gridBagConstraints);

        add(jPanel1, java.awt.BorderLayout.NORTH);

        tabTextArea.setFont(new java.awt.Font("Courier", 0, 12));
        jScrollPane1.setViewportView(tabTextArea);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel2.add(btnOpenFile);

        add(jPanel2, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void endSliderMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_endSliderMouseReleased
        if (endTimeSlider.getValue() > startTimeSlider.getValue()) {
            setTimeSliderEndPaused(true);
            btnPauseEnd.setText("Synch");
        }
    }//GEN-LAST:event_endSliderMouseReleased

    private void startSliderMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_startSliderMouseReleased
        setTimeSliderStartPaused(true);
        btnPauseStart.setText("Synch");
    }//GEN-LAST:event_startSliderMouseReleased

    private void btnLoopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoopActionPerformed
        if (endTimeSlider.getValue() > startTimeSlider.getValue()) {
            if (!isLooping()) {
                setLooping(true);
                btnLoop.setText("UnLoop");
                musicPlayerManager.loop(startTimeSlider.getValue(), endTimeSlider.getValue());
            } else {
                setLooping(false);
                btnLoop.setText("Loop");
                musicPlayerManager.loopOff();
            }
        }
    }//GEN-LAST:event_btnLoopActionPerformed

    private void btnPauseEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPauseEndActionPerformed
        if (isTimeSliderEndPaused()) {
            setTimeSliderEndPaused(false);
            btnPauseEnd.setText("Pause");
        } else {
            setTimeSliderEndPaused(true);
            btnPauseEnd.setText("Synch");
        }
    }//GEN-LAST:event_btnPauseEndActionPerformed

    private void btnPauseStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPauseStartActionPerformed
        if (isTimeSliderStartPaused()) {
            setTimeSliderStartPaused(false);
        } else {
            setTimeSliderStartPaused(true);
        }
    }//GEN-LAST:event_btnPauseStartActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLoop;
    private javax.swing.JButton btnOpenFile;
    private javax.swing.JButton btnPauseEnd;
    private javax.swing.JButton btnPauseStart;
    private javax.swing.JLabel endTimeLabel;
    private javax.swing.JSlider endTimeSlider;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel startTimeLabel;
    private javax.swing.JSlider startTimeSlider;
    private javax.swing.JTextArea tabTextArea;
    // End of variables declaration//GEN-END:variables

    public void setElapsedTrackTime(long time) {
        if (!isTimeSliderStartPaused()) {
            startTimeSlider.setValue((int) time);
        }
        if (!isTimeSliderEndPaused()) {
            endTimeSlider.setValue((int) time);
        }
    }

    public boolean isTimeSliderStartPaused() {
        return timeSliderStartPaused;
    }

    public void setTimeSliderStartPaused(boolean timeSliderStartPaused) {
        this.timeSliderStartPaused = timeSliderStartPaused;
        if (timeSliderStartPaused) {
            btnPauseStart.setText("Synch");
        } else {
            btnPauseStart.setText("Pause");
        }
    }

    public boolean isTimeSliderEndPaused() {
        return timeSliderEndPaused;
    }

    public void setTimeSliderEndPaused(boolean timeSliderEndPaused) {
        this.timeSliderEndPaused = timeSliderEndPaused;
        if (timeSliderEndPaused) {
            btnPauseEnd.setText("Synch");
        } else {
            btnPauseEnd.setText("Pause");
        }
    }

    private void setSongInfo(MPDSong song) {
        if (song != null) {
            startTimeSlider.setMaximum((int) song.getLength());
            endTimeSlider.setMaximum((int) song.getLength());
        }
    }

    private void verifyEndTime() {
        if (endTimeSlider.getValue() < startTimeSlider.getValue()) {
            endTimeSlider.setValue(startTimeSlider.getValue());
            setTimeSliderEndPaused(false);
        }
    }

    public boolean isLooping() {
        return looping;
    }

    public void setLooping(boolean looping) {
        this.looping = looping;
    }

    /**
     * @return the player
     */
    public MPDPlayer getPlayer() {
        return player;
    }

    /**
     * @param player the player to set
     */
    public void setPlayer(MPDPlayer player) {
        this.player = player;
    }

    public void playerChanged(PlayerChangeEvent event) {
        switch (event.getId()) {
            case PlayerChangeEvent.PLAYER_SONG_SET:
            case PlayerChangeEvent.PLAYER_STARTED:
                try {
                    setSongInfo(getPlayer().getCurrentSong());
                } catch (MPDConnectionException ex) {
                    Logger.getLogger(GuitarPanel.class.getName()).log(Level.SEVERE, null, ex);
                } catch (MPDPlayerException ex) {
                    Logger.getLogger(GuitarPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
        }
    }

    public void trackPositionChanged(TrackPositionChangeEvent event) {
        setElapsedTrackTime(event.getElapsedTime());
    }

    public void playlistBasicChange(PlaylistBasicChangeEvent event) {
        switch (event.getId()) {
            case PlaylistBasicChangeEvent.SONG_CHANGED:
                try {
                    setSongInfo(getPlayer().getCurrentSong());
                } catch (MPDConnectionException ex) {
                    Logger.getLogger(GuitarPanel.class.getName()).log(Level.SEVERE, null, ex);
                } catch (MPDPlayerException ex) {
                    Logger.getLogger(GuitarPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
        }
    }
}
